package com.bwie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Blog;
import com.bwie.service.BlogService;
import com.bwie.utils.R;
import com.bwie.vo.BlogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */

@RestController
@RequestMapping("blog")
@Api(tags = "博客微服务")
public class BlogController {


    @Autowired
    BlogService blogService;


    /**
    * //TODO 分页列表
    * @author 国兴越
    * @date 2024/1/9 8:56
 * @param pageNum
 * @param pageSize
 * @return com.bwie.utils.R
    */


    @ApiOperation("博客列表")
    @PostMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable(name = "pageNum")int pageNum,@PathVariable(name = "pageSize")int pageSize){
        Page<Blog> list =blogService.lists(pageNum,pageSize);
        return R.ok().message("博客列表查询成功").data("list",list);
    }


    /**
    * //TODO 模糊查询
    * @author 国兴越
    * @date 2024/1/9 8:56
 * @param name
 * @return com.bwie.utils.R
    */

    @ApiOperation("查询")
    @PostMapping("select/{name}")
    public R select(@PathVariable(name = "name")String name){

      List<Blog> list = blogService.select(name);

      return R.ok().message("查询成功").data("list",list);
    }


    /***
    * //TODO 添加
    * @author 国兴越
    * @date 2024/1/9 8:56
 * @param blogVo
 * @return com.bwie.utils.R
    */


    @ApiOperation("添加博客")
    @PostMapping("add")
    public R add(@RequestBody BlogVo blogVo){


        Blog blog = BeanUtil.toBean(blogVo, Blog.class);

        boolean save = blogService.save(blog);

        return save?R.ok().message("添加成功"):R.error().message("添加失败");

    }



    /***
    * //TODO 修改
    * @author 国兴越
    * @date 2024/1/9 8:56
 * @param blogVo
 * @return com.bwie.utils.R
    */


    @ApiOperation("修改博客")
    @PostMapping("update")
    public R update(@RequestBody BlogVo blogVo){


        Blog blog = BeanUtil.toBean(blogVo, Blog.class);

        boolean save = blogService.updateById(blog);

        return save?R.ok().message("修改成功"):R.error().message("修改失败");

    }

    /**
    * //TODO 删除
    * @author 国兴越
    * @date 2024/1/9 8:56
 * @param id
 * @return com.bwie.utils.R
    */


    @ApiOperation("删除")
    @PostMapping("rm/{id}")
    public R rm(@PathVariable(name = "id")Integer id){


        boolean b = blogService.removeById(id);

        return b?R.ok().message("删除成功"):R.error().message("删除失败");

    }



}
